package it.free.maya;

import java.util.Iterator;
import java.util.List;

import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.client.solrj.SolrQuery;

/**
 * @author Stefano Falconetti
 */
public class Test {

	static{
		//Cause configuration loading
		SearchEngineQueryProxy.getInstance();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/**
		 * Default OR is specified in schema.xml: <solrQueryParser defaultOperator="OR"/>
		 * Boolean operator can be: AND, OR, NOT  
		 */
		//Range query on fiels: product_price
		//String query = "product_price:[10 TO 500] AND all:(Canon AND cartucce)";//"all:(Canon AND cartucce)";
		String query = "all:Canon";
		
		try{
			long millisBeforeQuery = System.currentTimeMillis();
			FacetedSearchResult facetedSearchResult = SearchEngineQueryProxy.getInstance().executeQuery(query, 
																									    0, 
																									    20, 
																									    "product_price",
																									    SolrQuery.ORDER.desc,
																									    true,
																									    "product_name");
			
			SolrDocumentList solrDocumentList = facetedSearchResult.getSolrDocumentList();
			List<FacetField> facetedFields = facetedSearchResult.getFacetFields();
			
			long millisAfterQuery = System.currentTimeMillis() - millisBeforeQuery;
			
			System.out.println("Query milliseconds: " + millisAfterQuery + " for " + solrDocumentList.getNumFound() + " query results");
			
			Iterator<SolrDocument> solrDocumentsIterator = solrDocumentList.iterator();
			Iterator<FacetField> facetFieldsIterator = facetedFields.iterator();
			Iterator<Count> countIterator = null;
			Count count = null;
			
			SolrDocument solrDocument = null;
			FacetField facetField = null;
			int counter = 1;
			
			while(solrDocumentsIterator.hasNext()){
				  solrDocument = solrDocumentsIterator.next();
				  System.out.println("======== Product: " + (counter++) + " of " + solrDocumentList.getNumFound() + " ========");
				  System.out.println("sales_market=[" + solrDocument.getFieldValue("sales_market") + "]");
				  System.out.println("vendor_code=[" + solrDocument.getFieldValue("vendor_code") + "]");
				  System.out.println("product_code=[" + solrDocument.getFieldValue("product_code") + "]");
				  System.out.println("product_name=[" + solrDocument.getFieldValue("product_name") + "]");
				  System.out.println("product_brand=[" + solrDocument.getFieldValue("product_brand") + "]");
				  System.out.println("product_detail_url=[" + solrDocument.getFieldValue("product_detail_url") + "]");
				  System.out.println("product_description=[" + solrDocument.getFieldValue("product_description") + "]");
				  System.out.println("product_image_url=[" + solrDocument.getFieldValue("product_image_url") + "]");
				  System.out.println("product_price=[" + solrDocument.getFieldValue("product_price") + "]");
				  System.out.println("product_reserved_price=[" + solrDocument.getFieldValue("product_reserved_price") + "]");
				  System.out.println("product_availability=[" + solrDocument.getFieldValue("product_availability") + "]");
				  System.out.println("shipping_costs=[" + solrDocument.getFieldValue("shipping_costs") + "]");
				  System.out.println("shipping_rules=[" + solrDocument.getFieldValue("shipping_rules") + "]");
				  System.out.println("shipping_rules_url=[" + solrDocument.getFieldValue("shipping_rules_url") + "]");
				  System.out.println("vendor_name=[" + solrDocument.getFieldValue("vendor_name") + "]");
				  System.out.println("ecommerce_web_site=[" + solrDocument.getFieldValue("ecommerce_web_site") + "]");
				  System.out.println("vendor_logo_url=[" + solrDocument.getFieldValue("vendor_logo_url") + "]");
				  System.out.println("vendor_web_site_url=[" + solrDocument.getFieldValue("vendor_web_site_url") + "]");
				  System.out.println("vendor_address=[" + solrDocument.getFieldValue("vendor_address") + "]");
				  System.out.println("vendor_zip_code=[" + solrDocument.getFieldValue("vendor_zip_code") + "]");
				  System.out.println("vendor_city=[" + solrDocument.getFieldValue("vendor_city") + "]");
				  System.out.println("vendor_district=[" + solrDocument.getFieldValue("vendor_district") + "]");
				  System.out.println("vendor_contact_phone=[" + solrDocument.getFieldValue("vendor_contact_phone") + "]");
				  System.out.println("vendor_contact_email=[" + solrDocument.getFieldValue("vendor_contact_email") + "]");
				  System.out.println("vendor_country_code=[" + solrDocument.getFieldValue("vendor_country_code") + "]");
				  System.out.println("currency_symbol=[" + solrDocument.getFieldValue("currency_symbol") + "]");
				  System.out.println("currency_iso_code=[" + solrDocument.getFieldValue("currency_iso_code") + "]");
				  System.out.println("category_description=[" + solrDocument.getFieldValue("category_description") + "]");
				  System.out.println("subcategory_description=[" + solrDocument.getFieldValue("subcategory_description") + "]");
				  System.out.println("--------------------");
				  
				  while(facetFieldsIterator.hasNext()){
					    facetField = facetFieldsIterator.next();
					    countIterator = facetField.getValues().iterator();
					    while(countIterator.hasNext()){
					    	  count = countIterator.next();
					          System.out.println("Product category: [" + count.getName() + "], elements: [" + count.getCount() + "], query filter: [" + count.getAsFilterQuery() + "]");
					    }
				  }
				  
				  System.out.println("========");
			}		
		}catch(Exception exc){
			   exc.printStackTrace();
		}
	}

}
